package com.bwie.system.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bwie.common.constants.Constants;
import com.bwie.common.domain.request.UserReqSendDelMessage;
import com.bwie.common.exceptions.BizException;

import java.util.Objects;

/**
 * @Author Junchen_N1
 * @Date 2025/6/17 12:02
 * @ClassName RemoteMessageService
 * @Description TODO
 */
public class RemoteMessageService {

    public static final String HOST = "127.0.0.1:9016/message/";

    /**
     * 删除好友后远程调用message服务发消息通知
     * @param userReqSendDelMessage
     */
    public static void sendDelFriendMsg(UserReqSendDelMessage userReqSendDelMessage){
        String jsonResult = HttpUtil.createPost(HOST + "sendDelFriendMsg")
                .body(JSON.toJSONString(userReqSendDelMessage)).execute().body();
        getJsonObject(jsonResult);
    }

    /**
     * 判断服务状态是否熔断
     * @param jsonResult
     * @return
     */
    private static String getJsonObject(String jsonResult) {
        JSONObject jsonObject = JSONObject.parseObject(jsonResult);
        if (!Objects.equals(String.valueOf(Constants.SUCCESS), String.valueOf(jsonObject.get("code")))) {
            throw new BizException("服务器繁忙，请稍后重试");
        }
        return JSON.toJSONString(jsonObject.get("data"));
    }

}
